********************************************************************************
* Table A5
* 
* Use data from 2001-2014 for financial and clinical outcomes results 
********************************************************************************



*******
******* Settings and directories 
*******

** specify an output directory 
local fpath_output "/homes/nber/shruthi-dua51934/sacarny-DUA51934/shruthi-dua51934/replication_files/output_20230606"

** directory containing the main analytic file
local fpath_build_output = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/build/output"

** directory with survival and readmission code 
local fpath_code = "/disk/agedisk4/medicare.work/sacarny-DUA51934/shruthi-dua51934/replication_files/analysis_20230606"

*******
******* which sections of code to run?
*******
local prepare_data = 1
local run_main_regressions = 1
local combine_main_tables = 1
local clean_main_tables = 1




*******
******* specify RHS variables 
*******
*local main_vars "logfte lw_capinv_tot ladjcosts_w ldafny_price05 ladjrev_w profit_margin_w shdx rhdx snra rnra"
local main_vars "logfte lw_capinv_tot ladjcosts_w ldafny_price05 ladjrev_w profit_margin_w shdx rhdx"


*******
******* read and assemble the data 
*******
if `prepare_data' == 1{
	
	use "`fpath_build_output'/revisions_hosp_yr_panel_20230912.dta", clear 

	** clean up
	label var hospbd "Total beds"

	* reformat urbancbsa variable: 1 if rural 
	bys id: egen urban2 = max(urbancbsa)
	replace urbancbsa = urban2 
	replace urbancbsa = 1 - urbancbsa 
	label var urbancbsa "Share rural hospitals"

	* drop if missing hrrcode
	drop if missing(hrrcode)

	* numeric variable for the aha id 
	egen id2 = group(id)

	* total cost per adjusted discharge 
	gen adj_costs = totcost / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_costs "Adjusted costs per IP discharge (\$1 millions)" 
	winsor2 adj_costs, cuts(05 95) by(year)

	* total revenue per adjusted discharge 
	gen adj_revenue = income / ((1000000)*ipdischarges_adultped*(1 + (opcharge/ipcharge)))
	label var adj_revenue "Adjusted revenue per IP discharge (\$1 millions)"
	winsor2 adj_revenue, cuts(05 95) by(year)

	* log costs and revenue 
	gen ladjcosts_w = log((adj_costs_w))
	label var ladjcosts_w "Log(costs/bed)"

	gen ladjrev_w = log((adj_revenue_w))
	label var ladjcosts_w "Log(revenue/bed)"

	* dafny price index 
	winsor2 dafny_price, cuts(05 95) by(year)
	gen ldafny_price05 = log(dafny_price_w)
	label var ldafny_price05 "log(Dafny price index)"

	* winsorize profit margin in each year
	winsor2 profit_margin, cuts(05 95) by(year) label

	* capital investment 
	replace capinv_tot = capinv_tot/1000000
	label var capinv_tot "Capital investment \$millions"

	winsor2 capinv_tot, cuts(05 95) by(year) label
	gen lw_capinv_tot = log(1+capinv_tot_w)
	label var lw_capinv_tot "log(1+winsorized capital investment)"


	* scale FTE counts by beds 
	gen fte_per_bed = fte/hospbd
	label var fte_per_bed  "Full time employees per bed"

	* create log(fte)
	gen logfte = log(fte)
	label var logfte "log(FTE)"
	label var fte "Full Time Employees"


	********
	******** for the difference-in-difference analysis
	******** 
	**  restrict the sample: Legacy, Target, Other forprofits 
	keep if forprofit == 1 
	drop if target2 == 1 | acq_other == 1 

	label var ind08_11acq_legacy "Acquirer * 08-11"
	label var ind12_14target "Target * 12-14"

	* define post-treatment 
	gen postm = ( year >= 2008)
	gen postm_target = postm * target 
	gen postm_legacy = postm * acq_legacy 
	label var postm_target "Post 2008 * Target"
	label var postm_legacy "Post 2008 * Acquirer"

	* define interim
	gen interim = (year == 2007)
	gen interim_target = interim * target
	gen interim_legacy = interim * acq_legacy

	label var interim_target "Target 2007"
	label var interim_legacy "Acquirer 2007"

	* sanity check... 
	assert target2 + acq_other == 0 

	* event study
	* generate year dummies 
	tab year, gen(yr)

	*interact year dummies with acq legacy/ target indicators  
	foreach v of varlist yr* {
		gen target_`v' = target * `v'	
		gen legacy_`v' = acq_legacy * `v'

	}

	* clean up the labels on group * year interactions 
	foreach v of varlist target_yr* {
		local num = subinstr("`v'", "target_yr", "", 1) 
		local `num' %4.0f `num'
		local num = `num' + 1998
		label var `v' "Target * `num'"
	}
	foreach v of varlist legacy_yr* {
		local num = subinstr("`v'", "legacy_yr", "", 1) 
		local `num' %4.0f `num'
		local num = `num' + 1998
		label var `v' "Acquirer * `num'"
	}

	* drop dummy for 2006 (t-1)
	drop *yr8

	* gen year * hrr FEs
	egen yrhrr = group(year hrrcode)

	* gen year * HRR TREND
	gen yr_hrr_trend = year * hrrcode
}





******* 
*******  run the regressions 
*******  
if `run_main_regressions' == 1 {
	
	
	
foreach v in `main_vars' {
	di "****** Variable: `v'" 
	preserve
	
	
	** drop 2003 for capital investment results 
	if "`v'" == "lw_capinv_tot" {
		drop if year == 2003
	}
	
	** run the regressions 
	** survival and readmission are run in a seperate file
	if inlist("`v'", "shdx", "rhdx", "snra", "rnra") {
		quietly do "`fpath_code'/TableA5_survreadm.do"		
	}
	
	else {
		reghdfe `v' postm_legacy postm_target interim_legacy interim_target  if forprofit == 1, absorb(year id2) vce(cluster id2)
	
		regsave using "`fpath_output'/`v'_main_tablea5", p  addlabel(rhs, "`v'", spec, "main") replace 
	
	} /*end regression */ 
		
	restore 
}


}









*******
******* combine tables across variables and specifications 
*******
if `combine_main_tables' == 1 {
	clear
	save "`fpath_output'/main_tables_comb_tablea5.dta", emptyok replace 

	foreach v in `main_vars' {
	 
		clear 

		di"`v'" 
		use "`fpath_output'/`v'_main_tablea5", clear 
	
		append using "`fpath_output'/main_tables_comb_tablea5.dta"
		
		save "`fpath_output'/main_tables_comb_tablea5.dta", replace 		
	
	}
	
}





******* 
******* clean up the tables and save to excel 
******* 
******* spec options are main hrryr uniform hrrtrend
if `clean_main_tables' == 1 {
	
	foreach spec in main  { 
		use "`fpath_output'/`spec'_tables_comb_tablea5", clear
		drop if strpos(var, "_cons") | strpos(var, "interim") | strpos(var, "hrr")

		replace var = strtitle(subinstr(var, "postm_", "", 1))
		replace var = "Acquirer" if strpos(var, "Legacy")
		replace var = "Target" if strpos(var, "Target")

		cap drop _id
		cap drop r2 spec 
		foreach v in coef stderr pval {
		replace `v' = round(`v', .001)
	}

	gen asterisk = "***" if pval <= .01
	replace asterisk = "**" if pval > .01 & pval <= .05
	replace asterisk = "*" if pval >.05 & pval < .1
	gen paren1 = "("
	gen paren2 = ")"
	
	egen coef2 = concat(coef asterisk)
	egen stderr2 = concat(paren1 stderr paren2)

	drop coef stderr paren1 paren2 asterisk pval
	rename coef2 coef
	rename stderr2 stderr 

	reshape wide coef stderr , i(rhs N) j(var) string
	order N, last 

	rename rhs depvar

	rename coefAcquirer post_Acquirer
	rename coefTarget post_Target
	rename stderrAcquirer se_Acquirer
	rename stderrTarget se_Target



	replace depvar = "Discordant vendor count" if depvar == "discvendcount"
	replace depvar = "Log of costs per inpatient" if depvar == "ladjcosts_w"
	replace depvar = "Log of revenue per inpatient" if depvar == "ladjrev_w"
	replace depvar = "Log of price index" if depvar == "ldafny_price05"
	replace depvar = "Log of FTE" if depvar == "logfte"
	replace depvar = "Log of 1 + capital investment" if depvar == "lw_capinv_tot"
	replace depvar = "Profit margin" if depvar == "profit_margin_w"
	replace depvar = "Readmission (risk-adjusted)" if depvar == "rhdx"
	replace depvar = "Survival (risk-adjusted)" if depvar == "shdx"
	replace depvar = "Readmission (unadjusted)" if depvar == "rnra"
	replace depvar = "Survival (unadjusted)" if depvar == "snra"
	replace depvar = "Physician churn rate" if depvar == "wcost_churn"
	replace depvar = "Physician entry rate" if depvar == "wcost_entrants"
	replace depvar = "Physician exit rate" if depvar == "wcost_exits"

	gen order = 1 if depvar == "Discordant vendor count"
	replace order = 2 if depvar == "Physician entry rate"
	replace order = 3 if depvar == "Physician exit rate" 
	replace order = 4 if depvar == "Physician churn rate" 
	replace order = 5 if depvar == "Log of FTE" 
	replace order = 6 if depvar == "Log of 1 + capital investment" 
	replace order = 7 if depvar == "Log of costs per inpatient"
	replace order = 8 if depvar == "Log of price index" 
	replace order = 9 if depvar == "Log of revenue per inpatient" 
	replace order = 10 if depvar == "Profit margin" 
	replace order = 11 if depvar == "Survival (risk-adjusted)" 
	replace order = 12 if depvar == "Readmission (risk-adjusted)" 
	replace order = 13 if depvar == "Survival (unadjusted)" 
	replace order = 14 if depvar == "Readmission (unadjusted)" 
	sort order 
			
	drop order 
	
	export excel using "`fpath_output'/TableA5.xls", sheet("Main spec 01-14 samp", replace) firstrow(varlabel)
	}

}




